/**
 * Copyright (c) 2013-2020, tanghailan  (tanghailan@88.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <b>Helper.java</b></br>
 *
 * <pre></pre>
 *
 * @author tanghailan tanghailan@88.com
 * @date 2021年05月17日 16:04:47
 * @since JDK 1.8
 */
public class Helper {

  /**
   * 根据枚举的字符串获取枚举的值
   *
   * @param className 包名+类名
   * @return
   * @throws Exception
   */
  public static List<Map<String, Object>> getAllEnum(String className) throws Exception {
    // 得到枚举类对象
    Class<Enum> clazz = (Class<Enum>) Class.forName(className);
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    // 获取所有枚举实例
    Enum[] enumConstants = clazz.getEnumConstants();
    // 根据方法名获取方法
    Method getCode = clazz.getMethod("getCode");
    Method getMsg = clazz.getMethod("getMsg");
    Map<String, Object> map = null;
    for (Enum item : enumConstants) {
      map = new HashMap<String, Object>();
      // 执行枚举方法获得枚举实例对应的值
      map.put("code", getCode.invoke(item));
      map.put("msg", getMsg.invoke(item));
      list.add(map);
    }
    return list;
  }
}
